/**
 * Created by jx on 2018/4/12.
 */
export default{
  //数组 获取key数组
  arrayOpera(data, key){
    let array = [];
    data.forEach((i) => {
      array.push(i[key])
    });
    return array;
  },
  // 对象将空的字符串转为null
  objDeleteNull (obj) {
    if (obj) {
      for (var key in obj) {
        if (key) {
          if (obj[key] === '') {
            obj[key] = null;
          }
        }
      }
      return obj;
    }
    return obj;
  },
  // 对象去除字符串收尾空格
  objTrim (obj) {
    if (obj) {
      for (var key in obj) {
        if (key) {
          if (typeof obj[key] === 'number') {
            obj[key] = obj[key].toString();
          }
          if (typeof obj[key] === 'string'){
            obj[key] = obj[key].trim();
          }
        }
      }
      return obj;
    }
    return obj;
  },
  // 将数组转成对象
  arrayToObj (array, key, value) {
    let obj = {};
    let keys = key || 'key';
    let values = value || 'value';
    array.forEach((item, index) => {
      obj[item[keys]] = item[values];
    });
    return obj;
  },
  // 将数组转成对象
  arrayByKeyToObj (array, value) {
    let obj = {};
    array.forEach((item, index) => {
      obj[item.key] = item[value];
    })
    return obj;
  },
  // 求两个数组的并集
  arrayUnion (arr, brr) {
    for (let i = 0; i < arr.length; i++) {
      let temp = arr[i];
      for (let j = 0; j < brr.length; j++) {
        if (brr[j].skuName === temp.skuName) {
          temp.img = brr[j].img;
          brr.splice(j, 1);
        }
      }
    }
    return arr.concat(brr);
  },
  // 数组里面是否有重复元素
  isRepeat(arr, key){
    var hash = {};
    for (var i in arr) {
      if (hash[arr[i][key]])
        return true;
      hash[arr[i][key]] = true;
    }
    return false;
  },
  // 去除掉数组里面的空字符串
  arrayDeleteNull (array) {
    if (array && array.length != 0) {
      let arrList = [];
      for (var i = 0; i < array.length; i++) {
        if (array[i] !== null && array[i] !== undefined && array[i] !== '') {
          arrList.push(array[i]);
        }
      }
      return arrList;
    }
    return array;
  },
  // 通过类目Id遍历出类目文本
  byCateIdToCateText (cateId, cateArr) {
    let cateText = '';
    if (cateId && cateArr) {
      for (var j = 0; j < cateArr.length; j++) {
        if (cateArr[j].children) {
          for (var z = 0; z < cateArr[j].children.length; z++) {
            if (cateArr[j].children[z].children) {
              for (var k = 0; k < cateArr[j].children[z].children.length; k++) {
                if (cateArr[j].children[z].children[k].id === cateId) {
                  cateText = cateArr[j].text + '>' + cateArr[j].children[z].text + '>' + cateArr[j].children[z].children[k].text;
                }
              }
            } else {
              if (cateArr[j].children[z].id === cateId) {
                cateText = cateArr[j].text + '>' + cateArr[j].children[z].text;
              }
            }
          }
        } else {
          if (cateArr[j].id === cateId) {
            cateText = cateArr[j].text;
          }
        }
      }
      return cateText;
    }
  },
  // 通过类目树获取数据
  byCateIdToGetData (cateId, cateArr) {
    let objData = {
      oneList: [],
      twoList: [],
      thirdList: [],
      thirdIndex: '',
    };
    if (cateId && cateArr) {
      for (var j = 0; j < cateArr.length; j++) {
        if (cateArr[j].children) {
          for (var z = 0; z < cateArr[j].children.length; z++) {
            if (cateArr[j].children[z].children) {
              for (var k = 0; k < cateArr[j].children[z].children.length; k++) {
                if (cateArr[j].children[z].children[k].id === cateId) {
                  objData.oneList = cateArr[j];
                  objData.twoList = cateArr[j].children[z];
                  objData.thirdList = cateArr[j].children[z].children;
                  objData.thirdIndex = k;
                }
              }
            } else {
              if (cateArr[j].children.id == cateId) {
                objData.oneList = cateArr[j];
                objData.twoList = cateArr[j].children[z];
                objData.thirdList = [];
                objData.thirdIndex = 0;
              }
            }
          }
        } else {
          if (cateArr[j].id === cateId) {
            objData.oneList = cateArr[j];
            objData.twoList = [];
            objData.thirdList = [];
            objData.thirdIndex = 0;
          }
        }
      }
      return objData;
    }
  },
  //element  select组件 ie兼容
  readonly(){
    var input = document.querySelectorAll('input');
    var ieSelect = document.getElementsByClassName('ieSelect')[0];
    var ieInput = ieSelect ? ieSelect.getElementsByTagName('input')[0] : '';

    for (var i = 0; i < input.length; i++) {
      if (input[i].attributes.readonly) {
        input[i].setAttribute('unselectable', 'on');
      }
    }
    //点击input2次  --点击外面一次  --点击input 可开始输入搜索
    if (ieSelect) {
      ieSelect.addEventListener('click', function () {
        console.log('divclick')

        //element也有对input的js函数，所以延迟处理
        setTimeout(() => {
          if (ieInput.getAttribute('readonly')) {
            ieInput.setAttribute('readonly', 'readonly');
            ieInput.setAttribute('unselectable', 'on');
          } else {
            ieInput.removeAttribute('readonly');
            ieInput.removeAttribute('unselectable');
          }
        }, 2000)

      });
    }
  },
  formatFloat (f, digit) {
    var m = Math.pow(10, digit);
    return Math.round(f * m, 10) / m;
  },
  //判断浏览器类型
  getCurBrowser() {
    var webInfo = navigator.userAgent;
    var chromeVendor = navigator.vendor;
    if (webInfo.indexOf('OPR') !== -1) {
      return 'Opera';
    } else if (webInfo.indexOf('Edge') !== -1) {
      return 'Edge';
    } else if (webInfo.indexOf('Firefox') !== -1) {
      return 'FF';
    } else if (webInfo.indexOf('MSIE 9.0') !== -1) {
      return 'IE9';
    } else if (webInfo.indexOf('MSIE 10.0') !== -1) {
      return 'IE10';
    } else if (webInfo.indexOf('Trident') !== -1) {
      return 'IE11';
    } else if ((chromeVendor != '' && chromeVendor != undefined && chromeVendor != null && chromeVendor.indexOf('Google') !== -1) || webInfo.indexOf('Chrome') !== -1) {
      return 'Chrome';
    } else {
      return 'other';
    }
  },
  formatUnixtimestamp (unixtimestamp){
    var unixtimestamp = new Date(unixtimestamp*1000);
    var year = 1900 + unixtimestamp.getYear();
    var month = "0" + (unixtimestamp.getMonth() + 1);
    var date = "0" + unixtimestamp.getDate();
    var hour = "0" + unixtimestamp.getHours();
    var minute = "0" + unixtimestamp.getMinutes();
    var second = "0" + unixtimestamp.getSeconds();
    return year + "-" + month.substring(month.length-2, month.length)  + "-" + date.substring(date.length-2, date.length)
      + " " + hour.substring(hour.length-2, hour.length) + ":"
      + minute.substring(minute.length-2, minute.length) + ":"
      + second.substring(second.length-2, second.length);
  }
}
